package com.skt.controller;

import com.skt.domain.DTO.MerchantCostumeQueryDTO;
import com.skt.domain.DTO.request.MerchantCostumeCreateRequest;
import com.skt.domain.DTO.request.MerchantCostumeUpdateRequest;
import com.skt.domain.VO.MerchantCostumeCreateResultVO;
import com.skt.domain.VO.MerchantCostumeUpdateResultVO;
import com.skt.domain.VO.MerchantCostumeVO;
import com.skt.exception.BusinessException;
import com.skt.exception.CostumeErrorCode;
import com.skt.exception.CostumeException;
import com.skt.service.MerchantCostumesService;
import com.skt.utils.LogUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/merchant/costumes")
@Tag(name = "商家服装管理接口", description = "商家对租赁服装商品的增删改查管理")
public class MerchantCostumesController {

    private final MerchantCostumesService merchantCostumesService;

    @Operation(
            summary = "商家发布商品",
            description = "商家发布新的租赁商品，包含完整的商品信息录入和审核流程。商品初始状态为待审核，需平台审核通过后方可上架。"
    )
    @ApiResponses({
            @ApiResponse(responseCode = "201", description = "商品发布成功，进入待审核状态"),
            @ApiResponse(responseCode = "400", description = "请求参数错误，如必填字段缺失、格式错误"),
            @ApiResponse(responseCode = "401", description = "未授权访问，商家未登录或token失效"),
            @ApiResponse(responseCode = "403", description = "权限不足，商家无发布权限或账号异常"),
            @ApiResponse(responseCode = "409", description = "业务冲突，如商品数量超限、重复发布等"),
            @ApiResponse(responseCode = "500", description = "服务器内部处理错误")
    })
    @PostMapping
    public ResponseEntity<MerchantCostumeCreateResultVO> createCostume(
            @Parameter(description = "商品创建请求参数", required = true)
            @Valid @RequestBody MerchantCostumeCreateRequest createRequest) {

        LogUtil.setTraceId();
        LogUtil.startTimer("MERCHANT_COSTUME_CREATE");

        try {
            MerchantCostumeCreateResultVO result = merchantCostumesService.createCostume(createRequest);
            return ResponseEntity.status(HttpStatus.CREATED).body(result);
        } catch (BusinessException e) {
            LogUtil.error("MERCHANT_COSTUME", "CREATE", "商家商品发布业务异常", e);
            throw e;
        } catch (Exception e) {
            LogUtil.error("MERCHANT_COSTUME", "CREATE", "商家商品发布系统异常", e);
            throw CostumeException.of(CostumeErrorCode.CREATE_COSTUME_FAILED, "发布商品失败");
        } finally {
            LogUtil.stopTimer();
            LogUtil.clearContext();
        }
    }

    @Operation(
            summary = "商家编辑商品",
            description = "商家修改已发布的租赁商品信息。仅允许编辑处于“待审核”或“已下架”状态的商品；若商品当前为“已上架”状态，则修改后自动转为“待审核”，需重新通过平台审核后方可再次上架。"
    )
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "商品信息更新成功"),
            @ApiResponse(responseCode = "400", description = "请求参数错误，如必填字段缺失或格式错误"),
            @ApiResponse(responseCode = "401", description = "未授权访问，商家未登录或token失效"),
            @ApiResponse(responseCode = "403", description = "权限不足，非该商品所属商家或账号异常"),
            @ApiResponse(responseCode = "404", description = "商品不存在或已被删除"),
            @ApiResponse(responseCode = "409", description = "业务冲突，如尝试编辑不可编辑状态的商品"),
            @ApiResponse(responseCode = "500", description = "服务器内部处理错误")
    })
    @PutMapping("/{id}")
    public ResponseEntity<MerchantCostumeUpdateResultVO> updateCostume(
            @Parameter(description = "要编辑的商品ID", required = true, example = "12345")
            @PathVariable("id") Long id,

            @Parameter(description = "商品更新请求参数", required = true)
            @Valid @RequestBody MerchantCostumeUpdateRequest updateRequest) {

        LogUtil.setTraceId();
        LogUtil.startTimer("MERCHANT_COSTUME_UPDATE");

        try {
            MerchantCostumeUpdateResultVO result = merchantCostumesService.updateCostume(id, updateRequest);
            return ResponseEntity.ok(result);
        } catch (BusinessException e) {
            LogUtil.error("MERCHANT_COSTUME", "UPDATE", "商家商品编辑业务异常", e);
            throw e;
        } catch (Exception e) {
            LogUtil.error("MERCHANT_COSTUME", "UPDATE", "商家商品编辑系统异常", e);
            throw CostumeException.of(CostumeErrorCode.UPDATE_COSTUME_FAILED, "编辑商品失败");
        } finally {
            LogUtil.stopTimer();
            LogUtil.clearContext();
        }
    }

    @Operation(
            summary = "商家商品列表查询",
            description = "商家查询自己发布的所有租赁商品列表，支持按状态、分类、价格区间、时间范围等多维度筛选，并支持分页与排序。"
    )
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "成功返回商品列表"),
            @ApiResponse(responseCode = "400", description = "请求参数错误，如分页或状态值非法"),
            @ApiResponse(responseCode = "401", description = "未授权访问，商家未登录或token失效"),
            @ApiResponse(responseCode = "403", description = "权限不足，商家账号异常或非商家角色"),
            @ApiResponse(responseCode = "500", description = "服务器内部处理错误")
    })
    @GetMapping
    public ResponseEntity<List<MerchantCostumeVO>> listCostumes(
            @Parameter(description = "商品列表查询条件", required = false)
            @Valid MerchantCostumeQueryDTO queryDTO) {

        LogUtil.setTraceId();
        LogUtil.startTimer("MERCHANT_COSTUME_LIST");

        try {
            List<MerchantCostumeVO> result = merchantCostumesService.listCostumes(queryDTO);
            return ResponseEntity.ok(result);
        } catch (BusinessException e) {
            LogUtil.error("MERCHANT_COSTUME", "LIST", "商家商品列表查询业务异常", e);
            throw e;
        } catch (Exception e) {
            LogUtil.error("MERCHANT_COSTUME", "LIST", "商家商品列表查询系统异常", e);
            throw CostumeException.of(CostumeErrorCode.QUERY_MERCHANT_COSTUME_LIST_FAILED, "查询商家商品列表失败");
        } finally {
            LogUtil.stopTimer();
            LogUtil.clearContext();
        }
    }
}